package com.yonyou.pmclouds.safetypatrol.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.EnableStatusConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolDetailVO;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolVO;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolDetailMapper;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolMapper;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolDetailMaintain;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = SafetypatrolDetailMaintain.class)
public class SafetypatrolDetailMaintainImpl implements SafetypatrolDetailMaintain {
    @Resource
    private SafetypatrolDetailMapper safetypatrolDetailMapper;
    @Resource
    private SafetypatrolMapper safetypatrolMapper;
    @Resource
    private PictureRemoteService pictureRemoteService;




    //启用
    @Override
    public SafetypatrolDetailVO enableState(SafetypatrolDetailVO safetypatrolDetailVO) throws BusinessException {

        //这里要根据子表的外键查询主表的状态 所以需要进行非空校验
        if (StringUtils.isNotEmpty(safetypatrolDetailVO.getPkSafetypatroldetail())&&parentStatusDisable(safetypatrolDetailVO.getPkSafetypatrol())){
            throw new BusinessException("所选数据的父级目录为停用状态，请先启用父级目录。");
        }
        SingleUpdateBpTemplate<SafetypatrolDetailVO> singleUpdateBpTemplate =new SingleUpdateBpTemplate(safetypatrolDetailMapper,new String[]{"statecode"} );
        safetypatrolDetailVO.setStatecode(EnableStatusConst.ENABLE);
        singleUpdateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
        singleUpdateBpTemplate.update(safetypatrolDetailVO);
        //这个地方有锁 可以直接返回
        return safetypatrolDetailVO;

    }

    //停用
    @Override
    public SafetypatrolDetailVO disableState(SafetypatrolDetailVO safetypatrolDetailVO) throws BusinessException {

        if (StringUtils.isNotEmpty(safetypatrolDetailVO.getPkSafetypatroldetail())&&parentStatusDisable(safetypatrolDetailVO.getPkSafetypatrol())){
            throw new BusinessException("所选数据的父级目录为停用状态，请先启用父级目录。");
        }
        SingleUpdateBpTemplate<SafetypatrolDetailVO> singleUpdateBpTemplate =new SingleUpdateBpTemplate(safetypatrolDetailMapper,new String[]{"statecode"} );
        safetypatrolDetailVO.setStatecode(EnableStatusConst.DISABLE);
        singleUpdateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
        singleUpdateBpTemplate.update(safetypatrolDetailVO);

        return safetypatrolDetailVO;

    }

    @Override
    public void deleteDetailVO(SafetypatrolDetailVO safetypatrolDetailVO) throws BusinessException {
        SingleDeleteBpTemplate<SafetypatrolDetailVO> deleteDetailTemplate = new SingleDeleteBpTemplate<>(safetypatrolDetailMapper);
        deleteDetailTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
        //查看是否被引用 这边交给检查器做了
        deleteDetailTemplate.delete(safetypatrolDetailVO);
        pictureRemoteService.deleteExamplePicsOfEntity(safetypatrolDetailVO.getPkSafetypatroldetail());

    }

    private boolean parentStatusDisable(String pk_safetypatrol ){
        Map map =new HashMap();
        map.put("pk_safetypatrol", pk_safetypatrol);
        SafetypatrolVO safetypatrolVO =safetypatrolMapper.findById(map);
        //父级停用了
        if (safetypatrolVO!=null && safetypatrolVO.getStatecode()== EnableStatusConst.DISABLE){
            return true;
        }
        return false;
    }


}
